﻿using Common;
using Microsoft.Extensions.Configuration;
using System;
using System.Threading;
using System.Threading.Tasks;


namespace RedisClients
{
    class Program
    {
        static bool isStart = true;
        static void Main(string[] args)
        {
            try
            {
                var builder = new ConfigurationBuilder().AddCommandLine(args);
                var configuration = builder.Build();
                int time = Int32.Parse(configuration["time"]);


                Console.WriteLine("开始抢购");
                while (isStart)
                {
                    if (DateTime.Now.Minute == time)
                    {
                        isStart = !isStart;
                        RedisRepository context = new RedisRepository();
                        for (int i = 0; i < 100; i++)
                        {
                            int id = i;
                            Task.Run(() =>
                            {
                                Orders orders = new Orders()
                                {
                                    id = Guid.NewGuid(),
                                    CreateTime = DateTime.Now
                                };
                                string listKey = "orderList";
                                var id = context.AddToListTop(listKey, orders);
                                Console.WriteLine($"客户端:{id} GID:{orders.id} 下单完成");
                                Thread.Sleep(20);
                            });

                        }
                    }
                }
                Console.ReadLine();
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex);
            }
           
        }
    }
}
